
/* 
 * ----------------------
 * Includes
 * ----------------------
 */
 
 // Standard
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

// SDL
#include <SDL.h>
#include <SDL/SDL_ttf.h>

// GUI
#include "elder_gui.h"

// Context
#include "context.h"

/* 
 * ----------------------
 * Global variables
 * ----------------------
 */
 
 extern SDL_Surface* screen;
 extern context currentContext;
 	
 textElement textElements[] = {
 	
 /*	
 	 String					Font		Size	Color			(x,y,w,h)			Update function pointer
 */
 	{"Time left : %d",		gara,		16,		COLOR_RED,		{25,8,0,0},			&updateTime,		NULL, NULL, NULL},
 	{"%s",					gara,		20,		COLOR_GREEN,	{350,8,0,0},		&updateRun,			NULL, NULL, NULL},
 	{"Points : %dg",		gara,		16,		COLOR_BLUE,		{130,8,0,0},		&updatePoints,		NULL, NULL, NULL},
 	{"Backlight : %d/%d",	gara,		14,		COLOR_WHITE,	{50,200,0,00},		&updateBacklight,	NULL, NULL, NULL},
 	
 };
 
 /*
  * 
  * Functions
  * 
  */
  
  // Load all the fonts needed by the GUI
  int loadFonts(void)
  {
  	
  	int result = 0;
	int i = 0;
	TTF_Font** currentTable;
	unsigned char currentSize;
	char currentPath[FONT_PATH_MAX_LENGTH];
	
	int nb_texts = sizeof(textElements) / sizeof(textElement);
	
	TTF_Font* garaUsedSizes[FONT_MAX_SIZE+1];
	TTF_Font* digital7UsedSizes[FONT_MAX_SIZE+1];

	
	// Foreach different (font;size) couple of textElements table,
	// we create an associated TTF_Font
	// Then, address value is stored in a table where the index represent
	// the font's size (garaUsedSizes)
	
	// Initialisation of UsedSizes table
	for(i=0; i<FONT_MAX_SIZE+1; i++)
	{
		garaUsedSizes[i] = NULL;
		digital7UsedSizes[i] = NULL;
	}
	
	// Creation of the fonts
	for(i=0; i<nb_texts &&!result; i++)
	{
		
		switch(textElements[i].fontName)
		{
			case gara:
				currentTable = garaUsedSizes;
				strcpy(currentPath, FONT_GARA_PATH);		
			break;
			
			case digital7:
			default:
				currentTable = digital7UsedSizes;
				strcpy(currentPath, FONT_DIGITAL7_PATH);
			break;			
		}
		
		// Check if size is correct
		if(textElements[i].fontSize <= FONT_MAX_SIZE)
		{
			currentSize = textElements[i].fontSize;
			
		// Font size is too large	
		} else {
			currentSize = FONT_MAX_SIZE;
			printf("[SDL] Warning ! Font size %d is too large, truncated to %d\n",textElements[i].fontSize, FONT_MAX_SIZE);			
		}
		
		// If the font doesn't exist, we create it
		if(currentTable[currentSize] == NULL)
		{		
			if((currentTable[currentSize] = TTF_OpenFont(currentPath, textElements[i].fontSize)) == NULL)
			{
				fprintf(stderr,"[SDL] Unable to load fonts size %d %s \n",textElements[i].fontSize, TTF_GetError());
				result = -1;
			}
		}
		
		// And then store the fonts pointer
		textElements[i].font = currentTable[currentSize];
		
	}
			

	return result;

  }
  
  // Close all the fonts
  void closeFonts(void)
  {
  	int i = 0;
  	int nb_texts = sizeof(textElements) / sizeof(textElement);
  	
  	TTF_CloseFont(textElements[0].font);
  	TTF_CloseFont(textElements[1].font);
  	TTF_CloseFont(textElements[3].font);
  	
  	/*
  	for(i=0; i<nb_texts; i++)
  	{
  		
  		if(textElements[i].font)
  		{
  			printf("Closing %d... \n",textElements[i].font);
  			TTF_CloseFont(textElements[i].font);
  			printf("... %d close \n",textElements[i].font);
  			textElements[i].font = NULL;
  			printf("... %d done \n",textElements[i].font);
  		}
  	}*/
  }
  
  
  // Draw all textual elements
  void drawTextElements(void)
  {
  	int i = 0;
  	int nb_texts = sizeof(textElements) / sizeof(textElement);
  	
  	for(i=0; i<nb_texts; i++)
  	{
  		// Call the update function
  		if(textElements[i].updateFunc != NULL)
  		{
  			(textElements[i].updateFunc)(i);
  		}
  		  		
  		// Render the text
  		textElements[i].sText = TTF_RenderText_Blended(textElements[i].font, textElements[i].string, textElements[i].color);
  		SDL_BlitSurface(textElements[i].sText, NULL, screen, &(textElements[i].position));
  		SDL_FreeSurface(textElements[i].sText); 
  				
  	}
  	
  	SDL_Flip(screen);
  	
  }
  
  /*
   * --------------------
   * String update functions
   * --------------------
   */
   
  void updateTime(int index)
  {
  	sprintf(textElements[index].string, textElements[index].format, currentContext.timeLeft); 	
  }
  
  
  void updatePoints(int index)
  {
  	sprintf(textElements[index].string, textElements[index].format, 2237);	
  }
  
  void updateRun(int index)
  {
  	SDL_Color cRed = {0xFF,0x00,0x00};
  	SDL_Color cGreen = {0x00,0xFF,0x00};
  	
  	// If we run...
  	if(currentContext.run)
  	{
  		textElements[index].color = cGreen;
  		sprintf(textElements[index].string, textElements[index].format, "RUN : GOGO !");
  	} else {
  		textElements[index].color = cRed;
  		sprintf(textElements[index].string, textElements[index].format, "STOP BOY !");
  	}
  }
  
  void updateBacklight(int index)
  {
  	sprintf(textElements[index].string, textElements[index].format, currentContext.backlight, currentContext.maxBacklight);	
  }
 		

